#include "my_ui.h"


// 1. 全局变量：记录当前速度（初始值0km/h，仅当前文件可见）
 int timer_hours = 0;
 int timer_minutes = 0;
 int timer_seconds = 0;
// 定时器状态（0-停止，1-运行）
 int timer_running = 0;
float total_mileage = 0.0f;
float total_fuel_consumption = 0.0f;



void my_ui_init(void)
{
    lv_timer_create(time_update_event_cb, 1000, NULL);
    pthread_t thread_id;
    pthread_create(&thread_id,NULL,speed_routine,NULL);
    timer_init();
}
 void time_update_event_cb(lv_timer_t *timer) 
{
    time_t now;
    struct tm timeinfo;
    char time_str[20];
    char date_str[20];
    
    // 获取当前时间
    time(&now);
    localtime_r(&now, &timeinfo);
    
    // 格式化时间字符串 (HH:MM:SS)
    strftime(time_str, sizeof(time_str), "%H:%M:%S", &timeinfo);
    // 格式化日期字符串 (YYYY年MM月DD日)
    strftime(date_str, sizeof(date_str), "%Y年%m月%d日", &timeinfo);
    
    // 更新界面上的时间和日期标签
    lv_label_set_text(ui_time, time_str);
    lv_label_set_text(ui_date, date_str);
    lv_label_set_text(ui_time1, time_str);
    lv_label_set_text(ui_date1, date_str);
     lv_label_set_text(ui_time2, time_str);
    lv_label_set_text(ui_date2, date_str);
}

float update_mileage(float current_speed)
{
    // 只有当定时器运行且速度大于0时才计算里程
    if (timer_running && current_speed > 0) {
        // 1秒内行驶的距离（km）：速度(km/h) * (1/3600)小时
        float distance_per_second = current_speed / 3600.0f;
        total_mileage += distance_per_second;
    }
    
    return total_mileage;
}

void update_mileage_display(lv_obj_t *mileage_label)
{
    if (mileage_label == NULL) return;
    
    char mileage_str[20];
    // 格式化里程字符串，保留两位小数
    snprintf(mileage_str, sizeof(mileage_str), "%.2f", total_mileage);
    lv_label_set_text(ui_xingshilicheng, mileage_str);
}

void update_timer_display(void)
{
    char timer_str[20];
    // 格式化时间字符串 (HH:MM:SS)
    snprintf(timer_str, sizeof(timer_str), "%02d:%02d:%02d", 
             timer_hours, timer_minutes, timer_seconds);
    // 更新界面显示
    if(ui_xingshishijian!= NULL) {
        lv_label_set_text(ui_xingshishijian, timer_str);
    }
}

 void timer_count_event_cb(lv_timer_t *timer)
{
    if(!timer_running) return;

    // 秒累加
    timer_seconds++;
    
    // 进位处理
    if(timer_seconds >= 60) {
        timer_seconds = 0;
        timer_minutes++;
        
        if(timer_minutes >= 60) {
            timer_minutes = 0;
            timer_hours++;
            
            // 可根据需要添加小时上限处理
            // if(timer_hours >= 24) timer_hours = 0;
        }
    }
    
    // 更新显示
    update_timer_display();

    float current_speed = get_current_speed();
    
    // 更新里程并刷新显示（假设里程标签为ui_mileage）
    update_mileage(current_speed);
    update_mileage_display(ui_xingshilicheng);
    // 添加油耗更新
    update_fuel_consumption(); 
}

int get_current_speed(void)
{
    // 检查speedLabel2是否有效
    if (ui_speedLabel2 == NULL) {
        return 0; // 或根据实际需求返回默认值
    }
    
    // 获取标签的文本内容
    const char *speed_text = lv_label_get_text(ui_speedLabel2);
    if (speed_text == NULL || *speed_text == '\0') {
        return 0; // 文本为空时返回默认值
    }
    
    // 将字符串转换为整数（假设速度值为整数）
    return atoi(speed_text);
}

/**
 * 根据速度和定时器状态计算油耗并更新显示
 * 假设：油耗计算基于百公里油耗模型，此处假设一个基础百公里油耗为8L/100km
 * 实际应用中需根据具体车型的油耗模型调整计算方式
 */
void update_fuel_consumption(void)
{
    // 检查标签有效性
    if (ui_haoyouliang == NULL) return;

    // 获取当前速度
    float current_speed = get_current_speed();
    
    // 只有定时器运行且速度大于0时才计算油耗
    if (timer_running && current_speed > 0) {
        // 1. 计算当前小时的油耗（L/h）：(百公里油耗 / 100km) * 速度(km/h)
        // 此处假设基础百公里油耗为8L/100km，可根据实际情况调整
        float hourly_fuel = (8.0f / 100.0f) * current_speed;
        
        // 2. 计算1秒内的耗油量（L）
        float fuel_per_second = hourly_fuel / 3600.0f;
        
        // 3. 累加总耗油量
        total_fuel_consumption += fuel_per_second;
    }
    
    // 更新油耗显示（保留两位小数）
    char fuel_str[20];
    snprintf(fuel_str, sizeof(fuel_str), "%.2f L", total_fuel_consumption);
    lv_label_set_text(ui_haoyouliang, fuel_str);
}

void timer_init(void)
{
    // 创建1秒间隔的定时器
    lv_timer_create(timer_count_event_cb, 1000, NULL);
    // 初始化显示
    update_timer_display();
}


void timer_start(void)
{
    timer_running = 1;
}
// 停止定时器
void timer_stop(void)
{
    timer_running = 0;
}

// 重置定时器
void timer_reset(void)
{
    timer_hours = 0;
    timer_minutes = 0;
    timer_seconds = 0;
    update_timer_display();
}

void speedanim_Animation(lv_obj_t * TargetObject, int delay, int input_value)
{
     static int pre_value = 0;//上次转动的数值
    int current_value = input_value;//当前输入转动的数值
    int target_value = current_value - pre_value ;//当前实际转动的值
    int target_angle = target_value*2330/220;//当前实际转动的角度
    pre_value = current_value;

    ui_anim_user_data_t * PropertyAnimation_0_user_data = lv_mem_alloc(sizeof(ui_anim_user_data_t));
    PropertyAnimation_0_user_data->target = TargetObject;
    PropertyAnimation_0_user_data->val = -1;
    lv_anim_t PropertyAnimation_0;
    lv_anim_init(&PropertyAnimation_0);
    lv_anim_set_time(&PropertyAnimation_0, 1000);
    lv_anim_set_user_data(&PropertyAnimation_0, PropertyAnimation_0_user_data);
    lv_anim_set_custom_exec_cb(&PropertyAnimation_0, _ui_anim_callback_set_image_angle);

    lv_anim_set_values(&PropertyAnimation_0, 0, target_angle);
    lv_anim_set_path_cb(&PropertyAnimation_0, lv_anim_path_ease_in);
    lv_anim_set_delay(&PropertyAnimation_0, delay + 0);
    // lv_anim_set_deleted_cb(&PropertyAnimation_0, _ui_anim_callback_free_user_data);
    lv_anim_set_playback_time(&PropertyAnimation_0, 0);
    lv_anim_set_playback_delay(&PropertyAnimation_0, 0);
    lv_anim_set_repeat_count(&PropertyAnimation_0, 0);
    lv_anim_set_repeat_delay(&PropertyAnimation_0, 0);
    lv_anim_set_early_apply(&PropertyAnimation_0, false);
    lv_anim_set_get_value_cb(&PropertyAnimation_0, &_ui_anim_callback_get_image_angle);
    lv_anim_start(&PropertyAnimation_0);
}

void zhuangsuanim_Animation(lv_obj_t * TargetObject, int delay,int input_value)
{
     static int pre_value = 0;//上次转动的数值
    int current_value = input_value;//当前输入转动的数值
    int target_value = current_value - pre_value ;//当前实际转动的值
    int target_angle = target_value*2330/7000;//当前实际转动的角度
    pre_value = current_value;

    ui_anim_user_data_t * PropertyAnimation_0_user_data = lv_mem_alloc(sizeof(ui_anim_user_data_t));
    PropertyAnimation_0_user_data->target = TargetObject;
    PropertyAnimation_0_user_data->val = -1;
    lv_anim_t PropertyAnimation_0;
    lv_anim_init(&PropertyAnimation_0);
    lv_anim_set_time(&PropertyAnimation_0, 2000);
    lv_anim_set_user_data(&PropertyAnimation_0, PropertyAnimation_0_user_data);
    lv_anim_set_custom_exec_cb(&PropertyAnimation_0, _ui_anim_callback_set_image_angle);
    lv_anim_set_values(&PropertyAnimation_0, 0, target_angle);
    lv_anim_set_path_cb(&PropertyAnimation_0, lv_anim_path_linear);
    lv_anim_set_delay(&PropertyAnimation_0, delay + 0);
    // lv_anim_set_deleted_cb(&PropertyAnimation_0, _ui_anim_callback_free_user_data);
    lv_anim_set_playback_time(&PropertyAnimation_0, 0);
    lv_anim_set_playback_delay(&PropertyAnimation_0, 0);
    lv_anim_set_repeat_count(&PropertyAnimation_0, 0);
    lv_anim_set_repeat_delay(&PropertyAnimation_0, 0);
    lv_anim_set_early_apply(&PropertyAnimation_0, false);
    lv_anim_set_get_value_cb(&PropertyAnimation_0, &_ui_anim_callback_get_image_angle);
    lv_anim_start(&PropertyAnimation_0);

}

void youbiaoanin_Animation(lv_obj_t * TargetObject, int delay, int input_value)
{
     static int pre_value = 0;//上次转动的数值
    int current_value = input_value;//当前输入转动的数值
    int target_value = current_value - pre_value ;//当前实际转动的值
    int target_angle = target_value*-860/100;//当前实际转动的角度
    pre_value = current_value;

    ui_anim_user_data_t * PropertyAnimation_0_user_data = lv_mem_alloc(sizeof(ui_anim_user_data_t));
    PropertyAnimation_0_user_data->target = TargetObject;
    PropertyAnimation_0_user_data->val = -1;
    lv_anim_t PropertyAnimation_0;
    lv_anim_init(&PropertyAnimation_0);
    lv_anim_set_time(&PropertyAnimation_0, 1000);
    lv_anim_set_user_data(&PropertyAnimation_0, PropertyAnimation_0_user_data);
    lv_anim_set_custom_exec_cb(&PropertyAnimation_0, _ui_anim_callback_set_image_angle);
    lv_anim_set_values(&PropertyAnimation_0, 0, target_angle);
    lv_anim_set_path_cb(&PropertyAnimation_0, lv_anim_path_linear);
    lv_anim_set_delay(&PropertyAnimation_0, delay + 0);
    // lv_anim_set_deleted_cb(&PropertyAnimation_0, _ui_anim_callback_free_user_data);
    lv_anim_set_playback_time(&PropertyAnimation_0, 0);
    lv_anim_set_playback_delay(&PropertyAnimation_0, 0);
    lv_anim_set_repeat_count(&PropertyAnimation_0, 0);
    lv_anim_set_repeat_delay(&PropertyAnimation_0, 0);
    lv_anim_set_early_apply(&PropertyAnimation_0, false);
    lv_anim_set_get_value_cb(&PropertyAnimation_0, &_ui_anim_callback_get_image_angle);
    lv_anim_start(&PropertyAnimation_0);

}

void temperatureanim_Animation(lv_obj_t * TargetObject, int delay, int input_value)
{
     static int pre_value = 0;//上次转动的数值
    int current_value = input_value;//当前输入转动的数值
    int target_value = current_value - pre_value ;//当前实际转动的值
    int target_angle = target_value*855/80;//当前实际转动的角度
    pre_value = current_value;
    
    ui_anim_user_data_t * PropertyAnimation_0_user_data = lv_mem_alloc(sizeof(ui_anim_user_data_t));
    PropertyAnimation_0_user_data->target = TargetObject;
    PropertyAnimation_0_user_data->val = -1;
    lv_anim_t PropertyAnimation_0;
    lv_anim_init(&PropertyAnimation_0);
    lv_anim_set_time(&PropertyAnimation_0, 2000);
    lv_anim_set_user_data(&PropertyAnimation_0, PropertyAnimation_0_user_data);
    lv_anim_set_custom_exec_cb(&PropertyAnimation_0, _ui_anim_callback_set_image_angle);
    lv_anim_set_values(&PropertyAnimation_0, 0, target_angle);
    lv_anim_set_path_cb(&PropertyAnimation_0, lv_anim_path_linear);
    lv_anim_set_delay(&PropertyAnimation_0, delay + 0);
    // lv_anim_set_deleted_cb(&PropertyAnimation_0, _ui_anim_callback_free_user_data);
    lv_anim_set_playback_time(&PropertyAnimation_0, 0);
    lv_anim_set_playback_delay(&PropertyAnimation_0, 0);
    lv_anim_set_repeat_count(&PropertyAnimation_0, 0);
    lv_anim_set_repeat_delay(&PropertyAnimation_0, 0);
    lv_anim_set_early_apply(&PropertyAnimation_0, false);
    lv_anim_set_get_value_cb(&PropertyAnimation_0, &_ui_anim_callback_get_image_angle);
    lv_anim_start(&PropertyAnimation_0);

}
void ui_event_bntspeed(lv_event_t * e)
{
    lv_event_code_t event_code = lv_event_get_code(e);
    lv_obj_t * target = lv_event_get_target(e);
    if(event_code == LV_EVENT_CLICKED) {
        speedanim_Animation(ui_speedhand, 0,2330);
    }
}


void gongneng(lv_event_t * e)
{
	 // 1. 获取选中的文本和索引
        char selected_text[64];  // 缓冲区，足够容纳最长选项
        lv_dropdown_get_selected_str(ui_gongnengselect, selected_text, sizeof(selected_text));
        uint16_t selected_idx = lv_dropdown_get_selected(ui_gongnengselect);


        // 打印选中信息（调试用）
        LV_LOG_USER("选中选项: %s (索引: %d)", selected_text, selected_idx);
        
        // 2. 根据选中项执行不同逻辑（请在此添加你的代码）
        switch(selected_idx) {
            case 0:  // 场景模拟
                // 添加"场景模拟"选项的处理逻辑
                break;
            case 1:  // 左转
                // 添加"左转"选项的处理逻辑
				 leftLED_Animation(ui_leftLED, 0);
        		_ui_flag_modify(ui_leftLED, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_TOGGLE);
                break;
            case 2:  // 右转
                // 添加"右转"选项的处理逻辑
                turnRIGHT_Animation(ui_rightLED, 0);
                _ui_flag_modify(ui_rightLED, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_TOGGLE);
                break;
            case 3:  // 制动系统故障
                // 添加"制动系统故障"选项的处理逻辑
                _ui_flag_modify(ui_zhidohidden, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_TOGGLE);
                break;
            case 4:  // ABS异常
                // 添加"胎压异常"选项的处理逻辑
                _ui_flag_modify(ui_ABShidden, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_TOGGLE);
                break;
            case 5:  // 拉起手刹
                // 添加"拉起手刹"选项的处理逻辑
                _ui_flag_modify(ui_Phidden, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_TOGGLE);
                break;
            case 6:  // 转向助力系统故障
                // 添加"转向助力系统故障"选项的处理逻辑
                _ui_flag_modify(ui_zhuangxianghidden, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_TOGGLE);
                break;
            case 7:  // ESP系统故障
                // 添加"ESP系统故障"选项的处理逻辑
                _ui_flag_modify(ui_ESPhidden, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_TOGGLE);
                break;
            case 8:  // 打开后备箱
                _ui_flag_modify(ui_hobeixianghidden, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_TOGGLE);
                break;
            case 9:  // 
                _ui_flag_modify(ui_liangcenghidden, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_TOGGLE);
                break;
            case 10:  //
            _ui_flag_modify(ui_qiangaihidden, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_TOGGLE);
                break;
            case 11:
                _ui_flag_modify(ui_anquandaihidden, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_TOGGLE);
                break;
            case 12:
                _ui_flag_modify(ui_anquanhidden, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_TOGGLE);
                break;
            case 13:
                 leftLED_Animation(ui_leftLED, 0);
        		_ui_flag_modify(ui_leftLED, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_TOGGLE);
                 leftLED_Animation(ui_leftLED, 0);
        		_ui_flag_modify(ui_leftLED, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_TOGGLE);
                break;
            case 14:
                _ui_flag_modify(ui_yuanguanghidden, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_TOGGLE);
                break;
            case 15:
                _ui_flag_modify(ui_jinguanghidden, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_TOGGLE);
                break;
            case 16:
                _ui_flag_modify(ui_shikuodenghidden, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_TOGGLE);
                break;
            case 17:
                _ui_flag_modify(ui_wudenghidden, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_TOGGLE);

                break;
            
            default:
                LV_LOG_USER("未知选项");
                break;
			}
}

// 下拉选项事件处理回调函数
// void ui_event_gongnengselect(lv_event_t * e)
// {
//     lv_event_code_t event_code = lv_event_get_code(e);
//     lv_obj_t * target = lv_event_get_target(e);
//     if(event_code == LV_EVENT_VALUE_CHANGED) {
//         gongneng(e);
//     }
// }